home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATETIME.SWG / 0031_UNIXDATE Routines.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-21  |  4KB  |  108 lines

  1.  
  2. (* A public domain Turbo Pascal unit to convert between the date formats *)
  3. (* of DOS and Unix; by Robert Walking-Owl October 1993                   *)
  4.  
  5. unit UnixDate;
  6.  
  7. interface
  8.  
  9. function DosToUnixDate(DOSTime: LongInt): LongInt;
  10. function UnixToDosDate(UnixDate: LongInt): LongInt;
  11.  
  12. implementation
  13.   uses DOS;
  14.  
  15. function DosToUnixDate(DOSTime: LongInt): LongInt;
  16. const DaysInMonth: array[1..12] of word =
  17.   (30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  18. var i, j :    Word;
  19.     UnixDate: LongInt;
  20.     DTR:      DateTime;
  21. begin
  22.    UnPackTime(DOSTime,DTR);
  23.    UnixDate := 0;
  24.    UnixDate:=(DTR.year-1970)*365+((DTR.year-1971) div 4);
  25.    j:=pred(DTR.day);
  26.    if DTR.month<>1
  27.      then for i:=1 to pred(DTR.month) do j:=j+DaysInMonth[i];
  28.    if ((DTR.year mod 4)=0) and (DTR.month>2)
  29.      then inc(j);
  30.    UnixDate:=UnixDate+j; (* Add number of days this year *)
  31.    UnixDate:=(UnixDate*24)+DTR.hour;
  32.    UnixDate:=(UnixDate*60)+DTR.min;
  33.    UnixDate:=(UnixDate*60)+DTR.sec;
  34.    DosToUnixDate:=UnixDate;
  35. end;
  36.  
  37. function UnixToDosDate(UnixDate: LongInt): LongInt;
  38. const DaysInMonth: array[1..12] of word =
  39.   (30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  40. var i, j :    Word;
  41.     DTR:      DateTime;
  42.     DosTime:  LongInt;
  43. begin
  44.    DaysInMonth[2]:=28;
  45.    DTR.sec  := UnixDate mod 60;  UnixDate := UnixDate div 60;
  46.    DTR.min  := UnixDate mod 60;  UnixDate := UnixDate div 60;
  47.    DTR.hour := UnixDate mod 24;  UnixDate := UnixDate div 24;
  48.    DTR.day  := UnixDate mod 365; UnixDate := UnixDate div 365;
  49.    DTR.year := UnixDate+1970;
  50.    DTR.day  := 1+DTR.day-((DTR.year-1972) div 4);
  51.    if (DTR.day > (31+29)) and ((DTR.year mod 4)=0)
  52.      then inc(DaysInMonth[2]);
  53.    DTR.month:=1;
  54.    while DTR.day>DaysInMonth[DTR.Month]
  55.      do begin
  56.        DTR.day := DTR.day - DaysInMonth[DTR.Month];
  57.        inc(DTR.month)
  58.        end;
  59.    PackTime(DTR,DosTime);
  60.    UnixToDosDate:=DosTime;
  61. end;
  62.  
  63. end.
  64.  
  65.  
  66. This archive includes the Turbo Pascal source for a unit that will
  67. convert between Unix-file timestamps and DOS-file timestamps.
  68.  
  69. The advantage is that you can write software, such as archivers
  70. or archiver-utilities which can handle Unix-style dates and times.
  71. (Note many systems will store the data in BigEndian format, so
  72. you'll have to do a further bit of conversion.)
  73.  
  74. If the value is bigendian: Turbo Pascal includes the function Swap 
  75. for words.  To swap a long integer you'll have to reverse the bytes.
  76.  
  77. Both systems store a packed record of the date and time in a four
  78. byte long-integer (also called a double-word).
  79.  
  80.  
  81. DOS stores the date and time (of a file) actually as two packed words:
  82.                   
  83.                    Date:              Time:
  84.  
  85. Bit:        FEDCBA98 76543210  FEDCBA98 76543210
  86.             xxxxxxx. ........  ........ ........   Year - 1980
  87.             .......x xxx.....  ........ ........   Month     (1-12)
  88.             ........ ...xxxxx  ........ ........   Day       (1-31)
  89.  
  90.             ........ ........  xxxxx... ........   Hours     (0-23)
  91.             ........ ........  .....xxx xxx.....   Minutes   (0-59)
  92.             ........ ........  ........ ...xxxxx   Seconds/2 (0-29)
  93.                                                          
  94.  
  95. Unix stores the date as the number of seconds since January 1, 1970 UTC
  96. (Universal Coordinated Time = Grenwich Mean Time).  The is an _exact_
  97. number (not including leap seconds)--it accounts for months of 28 (or
  98. 29, for leap years), 30 and 31 days.
  99.  
  100. Note that some (Unix) software assumes your time is set for UTC and stores
  101. the date/time stamp blindly, while others attempt to figure out which
  102. time zone you're in and convert the time appropriately.  (This can be
  103. done if the TZ variable is set properly.)  So don't fret if you find the
  104. conversions a few hours off...
  105.  
  106.  
  107.  
  108.